package cn.com.thingo.shyintelligent.presenter;

import javax.inject.Inject;

import cn.com.thingo.shyintelligent.base.RxPresenter;
import cn.com.thingo.shyintelligent.base.contract.MainContract;
import cn.com.thingo.shyintelligent.model.DataManager;
import cn.com.thingo.shyintelligent.model.bean.CommonBean;
import cn.com.thingo.shyintelligent.util.LogUtil;
import cn.com.thingo.shyintelligent.util.RxUtil;
import cn.com.thingo.shyintelligent.util.ToastUtil;
import io.reactivex.functions.Consumer;


public class MainPresenter extends RxPresenter<MainContract.View> implements MainContract.Presenter {

    private DataManager dataManager;

    @Inject
    public MainPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }


    @Override
    public void addMessage(String token, String title, String content) {
        addSubscribe(dataManager.addMessage(token, title, content)
                .compose(RxUtil.<CommonBean>rxSchedulerHelper())
                .subscribe(new Consumer<CommonBean>() {
                    @Override
                    public void accept(CommonBean commonBean) throws Exception {
                        if (commonBean.getCode() == 0) {
                            mView.viewAddMessage(commonBean);
                        } else {
                            ToastUtil.shortShow(commonBean.getMsg());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.e(throwable.getMessage());
                    }
                }));

    }
}
